#include "Trash.h"

void Trash::updateOperation()
{
	int i;
	stack <int> temp;
	for (i = 0; i < maxnoperation; i ++){
		temp.push(Operation.top());
		Operation.pop();
	}
	
	Operation.pop();

	for (i = 0; i < maxnoperation; i ++) {
		Operation.push(temp.top());
		temp.pop();
	}
}

void Trash::updateRedoOperation()
{
	int i;
	stack <int> temp;
	for (i = 0; i < 10; i ++){
		temp.push(RedoOperation.top());
		RedoOperation.pop();
	}
	
	RedoOperation.pop();

	for (i = 0; i < 10; i ++) {
		RedoOperation.push(temp.top());
		temp.pop();
	}
}

void Trash::add_Expense( XpensItem origin, XpensItem current)
{
	UndoExpense.push ( pair< XpensItem, XpensItem >( origin, current ) );
	if (Operation.size() > maxnoperation){
		updateOperation();

		stack < pair <XpensItem, XpensItem> > temp;
		int i;
		while (!UndoExpense.empty()){
			temp.push(UndoExpense.top());
			UndoExpense.pop();
		}

		temp.pop();

		while ( !temp.empty()){
			UndoExpense.push(temp.top());
			temp.pop();
		}
	}
}

void Trash::add_Payment(PaymentItem origin, PaymentItem current){
	UndoPayment.push ( pair< PaymentItem, PaymentItem >( origin, current ) );
	if (Operation.size() > maxnoperation){
		updateOperation();

		stack < pair <PaymentItem, PaymentItem> > temp;
		int i;
		while (!UndoPayment.empty()){
			temp.push(UndoPayment.top());
			UndoPayment.pop();
		}

		temp.pop();

		while ( !temp.empty()){
			UndoPayment.push(temp.top());
			temp.pop();
		}
	}
}

void Trash::addOperation( int operation)
{
	Operation.push(operation);
}

/*void Trash::add_lastIncome( double addValue)
{
	Income.push(addValue);
}

double Trash::getIncome()
{
	double target = Income.top();
	UndoIncome.push(target);
	Income.pop();
	return target;
}

double Trash::getUndoIncome()
{
	double target = UndoIncome.top();
	UndoIncome.pop();
	return target;
}*/

XpensItem Trash::getNewExpense()
{
	return UndoExpense.top().second;
}

XpensItem Trash::getOriginExpense()
{
	XpensItem target = UndoExpense.top().first;
	RedoExpense.push(UndoExpense.top());
	UndoExpense.pop();
	if ( RedoOperation.size() > maxnoperation )
	{
		updateOperation();

		stack < pair <XpensItem, XpensItem> > temp;
		int i;
		while (!RedoExpense.empty()){
			temp.push(RedoExpense.top());
			RedoExpense.pop();
		}

		temp.pop();

		while ( !temp.empty()){
			RedoExpense.push(temp.top());
			temp.pop();
		}
	}
	return target;
}

int Trash::getOperation()
{
	int target = Operation.top();
	RedoOperation.push(target);
	Operation.pop();
	return target;
}

int Trash::getRedoOperation()
{
	int target = RedoOperation.top();
	RedoOperation.pop();
	Operation.push(target);
	return target;
}

XpensItem Trash::getRedoNewExpense()
{
	return RedoExpense.top().first;
}

XpensItem Trash::getRedoOriginExpense()
{
	XpensItem target = RedoExpense.top().second;
	add_Expense(RedoExpense.top().first,RedoExpense.top().second);
	RedoExpense.pop();
	return target;
}

int Trash::getRedoOperation()
{
	return RedoOperation.top();
}

PaymentItem Trash::getOriginPayment()
{
	PaymentItem target = UndoPayment.top().first;
	RedoPayment.push(UndoPayment.top());
	UndoPayment.pop();
	if (RedoOperation.size() > 10) {
	stack < pair <PaymentItem, PaymentItem> > temp;
		int i;
		while (!RedoPayment.empty()){
			temp.push(RedoPayment.top());
			RedoPayment.pop();
		}

		temp.pop();

		while ( !temp.empty()){
			RedoPayment.push(temp.top());
			temp.pop();
		}
	}
	return target;
}

PaymentItem Trash::getNewPayment()
{
	return UndoPayment.top().second;
}

PaymentItem Trash::getRedoNewPayment()
{
	return RedoPayment.top().second;
}

PaymentItem Trash::getRedoOriginPayment()
{
	PaymentItem target = RedoPayment.top().second;
	add_Payment(RedoPayment.top().first, RedoPayment.top().second);
	RedoPayment.pop();
	return target;
}